<?php

namespace App\Http\Controllers\Admin\Play;

use App\Models\Play;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PlayController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.play.index');
    }

    /**
     * 异步获取列表数据
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
     */
    public function lists(Request $request)
    {
        if ($request->ajax()) {
            $offset = intval($request->get('start'));

            $paginate = Play::select(['id', 'user_id', 'category_id', 'title', 'is_template', 'views', 'created_at', 'updated_at'])->orderBy('id', 'desc')
                ->paginate(self::PAGE_SIZE, ['*'], 'page', intval($offset / self::PAGE_SIZE) + 1);

            $data = [];
            foreach ($paginate as $item) {

                $data[] = [
                    $item->id,
                    $item->user->username,
                    $item->category->title,
                    $item->title,
                    $item->is_template ? '是' : '否',
                    $item->views,
                    $item->created_at->toDateTimeString(),
                    $item->updated_at->toDateTimeString(),
                    '<a type="button" class="btn btn-primary btn-xs" target="_blank" href="http://thearte.jumpcity.cn/view?id='. $item->id .'">查看</a>',
                ];
            }

            $records = [
                'data' => $data,
                'draw' => intval($request->get('draw')),
                'recordsTotal' => $paginate->total(),
                'recordsFiltered' => $paginate->total(),
            ];

            return response()->json($records);
        } else {
            return redirect()->guest('admin/login');
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
